package day12.homework;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//参考课上讲述的定长线程池的案例 通过可缓存线程池 去完成下列需求
//现在餐厅生意越来越好了，老板直接跟大厨说，你能炒多快，就炒多快，灶台随时可以加，好好干，工资不会少。
//同样用线程池模拟炒五个菜的过程，并且统计炒完共计花费了多久
public class TestCache {
    public static void main(String[] args) {
        String[] names = {"辣椒炒肉","黄豆焖鸡爪","小鸡炖蘑菇","爆辣甲鱼","红烧大虾"};
        long[] times = {1000,3000,5000,5000,2000};
        ExecutorService es = Executors.newCachedThreadPool();
        long start = System.currentTimeMillis();
        long end = 0;
        for (int i = 0; i < 5; i++) {
            int index = i;
            es.execute(()->{
                try {
                    System.out.println("开始做菜:"+names[index]);
                    Thread.sleep(times[index]);
                    System.out.println("已经做好:"+names[index]);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        es.shutdown();
        while (true){
            if (es.isTerminated()){
                end = System.currentTimeMillis();
                break;
            }
        }
        System.out.println("炒菜一共花费"+(end-start)+"ms");
    }
}
